import axios from 'axios'
import qs from 'qs'
import { useNotif } from '../hooks/message'
import { message } from 'ant-design-vue';
import router from '@/router'
const baseURL = import.meta.env.VITE_BASE_URL
declare module 'axios' {
  interface AxiosInstance {
    (config: AxiosRequestConfig): Promise<any>
  }
}

const service = axios.create({
  timeout: 60000, //超时时间
  baseURL: baseURL, // 我们在请求接口的时候就不同写前面 会自动我们补全
})
// http request 拦截器
service.interceptors.request.use(
  (config) => {
    debugger
    config.headers!['token'] = localStorage.getItem('token')
    if (config.method === 'post') {
      config.data = qs.stringify(config.data)
    }
    return config
  },
  (err) => {
    return err
  }
)
// http response 拦截器
service.interceptors.response.use(
  (response) => {
    debugger
    let token = response.headers['token'];
    localStorage.setItem("token",token || "");

    if(response.data.result.code == '3000'){
      message.warn('token失效')
      router.push('/login')
    }

    if (response.data.code == 202) {
      //useNotif(response.data.msg, 'warning')
    }
    return response.data
  },
  (Error) => {
    debugger
    message.error('接口请求失败：' + Error?.response?.data?.data?.message)
    //return useNotif('登录异常', 'info', true)
  }
)
export default service
